package com.gfwy.proxy;

import java.util.List;

import com.gfwy.dao.IChapterDao;
import com.gfwy.dao.impl.ChapterDaoImpl;
import com.gfwy.dbc.DatabaseConnection;
import com.gfwy.vo.Chapter;

public class ChapterDaoProxy implements IChapterDao {
	IChapterDao dao = null;
	DatabaseConnection dbc = null;
	public ChapterDaoProxy() {
		try {
			this.dbc = new DatabaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.dao = new ChapterDaoImpl(this.dbc.getConnection());
	}	
	@Override
	public boolean doCreate(Chapter chapter) throws Exception {
		boolean flag = false;
		try {
			flag = this.dao.doCreate(chapter);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public boolean doUpdate(Chapter chapter) throws Exception {
		boolean flag = false;
		try {
			flag = this.dao.doUpdate(chapter);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public boolean doDelete(int cid) throws Exception {
		boolean flag = false;
		try {
			flag = this.dao.doDelete(cid);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return flag;
	}

	@Override
	public List<Chapter> findAll(String keyWord, int startIndex, int count)
			throws Exception {
		List<Chapter> list = null;
		try {
			list = this.dao.findAll(keyWord, startIndex, count);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return list;
	}

	@Override
	public int count(String keyWord) throws Exception {
		int count = 0;
		try {
			count = this.dao.count(keyWord);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return count;
	}

	@Override
	public Chapter findById(int cid) throws Exception {
		Chapter chapter = null;
		try {
			chapter = this.dao.findById(cid);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return chapter;
	}

	@Override
	public List<Chapter> findByPid(int pid) throws Exception {
		List<Chapter> list = null;
		try {
			list = this.dao.findByPid(pid);
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return list;
	}

}
